package com.luguangtao.controller;

import com.luguangtao.entity.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luguangtao.entity.UserExample;
import com.luguangtao.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @类名: com.luguangtao.controller
 * @作者: lgt
 * @创建时间: 2019-12-09 09:11
 * @描述:
 **/
@RestController
public class UserController1 {
    @Autowired
    UserMapper userMapper;

    //登录
    @RequestMapping("login1")
    public String login1(@RequestParam String username,@RequestParam String password){
        System.out.println(username+"********"+password);
        //后台加密
        //password = Md5Util.md5Encoding(password);
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(password);
        List<User> users = userMapper.selectByExample(userExample);
        if (users.size()>0 && users!=null){
            return "success";
        }else {
            return "error";
        }
    }

    //注册
    @RequestMapping("regist1")
    public int regist1(@RequestBody User user){
        //密码加密
        //user.setPassword(Md5Util.md5Encoding(user.getPassword()));
        user.setBirthday(user.getBirthday().substring(0,10));//截取日期
        int aa = userMapper.insert(user);
        if(aa!=0){
            return 1;
        }else {
            return 0;
        }
    }

    //列表
    @RequestMapping("lists1")
    public PageInfo<User> lists1(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize,@RequestParam String names){
        PageHelper.startPage(pageNum,pageSize);
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        //模糊  用户姓名
        if(!StringUtils.isEmpty(names)){
            criteria.andNameLike('%'+names+'%');
        }
        List<User> users = userMapper.selectByExample(userExample);
        PageInfo<User> userPageInfo = new PageInfo<>(users);
        return userPageInfo;
    }
}
